/**
 * 
 */
package org.sipsl.gisystems.gentest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.sipsl.gisystems.gentest.RteDataTestUnit;
import org.sipsl.gisystems.gentest.RteVoiceMoTestUnit;
import org.sipsl.gisystems.gentest.rte.util.CircularArrayList;
import org.sipsl.gisystems.gentest.rte.util.DataParams;
import org.sipsl.gisystems.gentest.rte.util.GenericUtil;
import org.sipsl.gisystems.gentest.rte.util.VoiceMoParams;
import org.sipsl.gisystems.gentest.rte.util.license.GetLicense;
import org.sipsl.gisystems.rte.V2.TestsuiteV2;
import org.sipsl.gisystems.rte.V2.TestsuiteV2.SubscriberType;
import org.sipsl.gisystems.rte.V2.TestsuiteV2.DataBatchGroup.Zone.DataBatch;
import org.sipsl.gisystems.rte.V2.TestsuiteV2.DataBatchGroup.Zone.DataBatch.DataTariff;
import org.sipsl.gisystems.rte.V2.TestsuiteV2.SubscriberType.Rteoptions;
import org.sipsl.gisystems.rte.V2.TestsuiteV2.DataBatchGroup.Caller;
import org.sipsl.gisystems.rte.V2.TestsuiteV2.VoiceBatchGroup.Called;
import org.sipsl.gisystems.rte.V2.TestsuiteV2.VoiceBatchGroup.Duration;
import org.sipsl.gisystems.rte.V2.TestsuiteV2.VoiceBatchGroup.Zone;
import org.sipsl.gisystems.rte.V2.TestsuiteV2.VoiceBatchGroup.Zone.VoiceBatch;
import org.sipsl.gisystems.rte.V2.TestsuiteV2.VoiceBatchGroup.Zone.VoiceBatch.VoiceTariff;
import org.sipsl.gisystems.rte.def.Rtedefaults;
import org.sipsl.gisystems.rte.def.Rtedefaults.Properties.PropertyKey;
import org.sipsl.gisystems.rte.def.Rtedefaults.Properties.PropertyKey.PropertyValue;

import com.rits.cloning.Cloner;
 
/**
 * @author incisadicameranag
 * 
 *         http://www.xmlforasp.net/CodeBank/System_Xml_Schema/BuildSchema/
 *         BuildXMLSchema.aspx
 * 
 *         guic@groog:~/DEV/wsRteGen/P1/src$ ../../../jaxb-ri-2.2.6/bin/xjc.sh
 *         ../rteDictionaryV2.xsd -p org.sipsl.gisystems.rte.V2
 */
public class GenTest {

	/**
	 * @param args
	 */

	private static String fileName;
	private static String publicKey;
	private static String defaultXml;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// create the command line parser
		CommandLineParser parser = new GnuParser();

		// create the Options
		Options options = new Options();
		options.addOption("k", "key", true, "public.key file path");
		options.addOption("d", "default", true, "default.xml file path");
		options.addOption("s", "surepay", true, "surepay.xml file path");

		try {
			CommandLine line = parser.parse(options, args);

			if (line.hasOption("k")) {
				publicKey = line.getOptionValue("k");
			}
			if (line.hasOption("d")) {
				defaultXml = line.getOptionValue("d");
			}
			if (line.hasOption("s")) {
				GenericUtil.LOG(line.getOptionValue("s"), 1, 0);
			}
			if (line.getArgs().length == 0) {
				fileName = "TESTING/DATA/CAP1/CAP1.xml";
				// fileName = "TESTING/VOICE/RANGE/RANGE.xml";

			} else {
				fileName = line.getArgs()[0];
			}

			GenericUtil.setLevel(1);
			buildTest(fileName);
			GenericUtil.LOG("*************************************");
			GenericUtil.LOG("*************************************");
		} catch (ParseException exp) {
			System.out.println("Unexpected exception:" + exp.getMessage());
		}

	}

	private static void buildTest(String fileName) {

		int ShowMe = 0;
		int ShowZone = 0;

		try {

			JAXBContext cate = JAXBContext
					.newInstance("org.sipsl.gisystems.rte.V2");
			Unmarshaller u = cate.createUnmarshaller();
			TestsuiteV2 bix = (TestsuiteV2) u.unmarshal(new FileInputStream(
					fileName));

			JAXBContext catesurepay = JAXBContext
					.newInstance("org.sipsl.gisystems.rte.V2");
			Unmarshaller usurepay = catesurepay.createUnmarshaller();
			TestsuiteV2 surepayReference = (TestsuiteV2) usurepay
					.unmarshal(new FileInputStream("surepay.xml"));

			Cloner cloner = new Cloner();

			String TestSuiteName = bix.getId();

			Map<String, String> params = new HashMap<String, String>();
			if (defaultXml == null) {
				defaultXml = bix.getDefaultdata().getFile();
			}
			fetchDefaults(params, defaultXml);

			// GetLicense.isValid(params.get("LICENSEKEY"), publicKey);

			int testNumber = 0;
			int batch = 0;
			int numTestInDir = 0;
			int subBatch = 0;
			String batchDir = "";
			if (bix.getDataBatchGroup() != null) {

				boolean isNew = false; // creation of testDocument

				for (Caller callerRange : bix.getDataBatchGroup().getCaller()) {

					Caller theCaller = null;
					int elementNumber = 0;
					int capCaller = 0;

					ArrayList<Caller> CiArLi = null;
					if (callerRange.getRange() != null) {
						CiArLi = getRange(callerRange);
						capCaller = CiArLi.size();
					} else {
						theCaller = callerRange;
					}

					for (org.sipsl.gisystems.rte.V2.TestsuiteV2.DataBatchGroup.Zone z : bix
							.getDataBatchGroup().getZone()) {


						for (DataBatch dtb : z.getDataBatch()) {

							ArrayList<String> eusgsn = new ArrayList<String>();
							loadNeZone(z.getId(), eusgsn, "DATA");

							// ArrayList<Caller> CiArLi = null;
							// if ( callerRange.getRange()!=null){
							// CiArLi = getRange(callerRange);
							// capCaller = CiArLi.size();
							// }else{
							// theCaller = callerRange;
							// }

							// params.put("MSISDN", theCaller.getMsisdn()
							// .toPlainString());
							// params.put("IMSI",
							// theCaller.getImsi().toPlainString());
							batchDir = GenericUtil.MakePrefix(batch) + ":"
									+ subBatch + "" + bix.getId() + ""
									+ dtb.getId();

							new File(batchDir).mkdir();
							subBatch = 0;
							numTestInDir = 0;
							batch++;

							List<DataTariff> dt = new ArrayList<DataTariff>();
							// replace from surepay.xml if reference
							if (dtb.getDataTariff().get(0).getReference() != null) {
								for (DataBatch dts : surepayReference
										.getDataBatchGroup().getZone().get(0)
										.getDataBatch()) {
									if (dts.getDataTariff().size() == 0) {
										continue;
									}
									if (dts.getDataTariff()
											.get(0)
											.getReference()
											.compareTo(
													dtb.getDataTariff().get(0)
															.getReference()) == 0) {
										int sub = dts.getDataTariff().size();
										DataBatch tts = cloner.deepClone(dts);
										dt = tts.getDataTariff()
												.subList(1, sub);
										break;
									}
								}
							} else {
								dt = dtb.getDataTariff();
							}

							params.put("APN", dtb.getApn());
							params.put("RULEBASE_ID", dtb.getRulbid());

							// parse category list
							String cats = (dtb.getCategoryid() != null) ? dtb
									.getCategoryid() : "0";
							for (String catId : cats.split(",")) {

								params.put("CATEGORY_ID", catId);

								for (String st : eusgsn) {
									boolean singleTest = false;
									String s[] = new String[2];
									s = st.split("#");
									params.put("SGSN", s[1]);

									int StartIndex = 1;
									{
										if (enqRteOption(bix,
												"OVERRIDE_DEFAULT_SIZE")) {
											StartIndex = 4;
										}
										// let's always skip default
										StartIndex = 4;

										for (int ii = StartIndex; ii <= 3 + bix
												.getDataBatchGroup()
												.getDatacall().size(); ii++) {

											if (CiArLi != null) {
												theCaller = CiArLi
														.get(elementNumber);
												elementNumber++;
												elementNumber = elementNumber
														% capCaller;
												params.put("MSISDN", theCaller
														.getMsisdn()
														.toPlainString());
												params.put("IMSI", theCaller
														.getImsi()
														.toPlainString());
											} else {
												params.put("MSISDN", theCaller
														.getMsisdn()
														.toPlainString());
												params.put("IMSI", theCaller
														.getImsi()
														.toPlainString());
											}

											DataParams dataParm = new DataParams(
													params);
											GenericUtil.LOG("[", 1, 1);
											GenericUtil
													.LOG("TEST "
															+ GenericUtil
																	.MakePrefix(testNumber)
															+ ":"
															+ TestSuiteName
															+ "" + dtb.getId()
															+ "" + z.getId()
															+ s[0] + ""
															+ testNumber);
											// get Next Caller

											singleTest = (z.getSingleTest() != null);

											RteDataTestUnit rte = new RteDataTestUnit(
													bix, surepayReference,
													dataParm, ii, dt, 0, dtb,
													bix.getDataBatchGroup()
															.getDatacall(),
													bix.getSubscriberType(),
													cloner, singleTest, eusgsn);

											numTestInDir++;
											if (numTestInDir > 20) {
												subBatch++;
												batchDir = GenericUtil
														.MakePrefix(batch)
														+ ":"
														+ subBatch
														+ ""
														+ bix.getId()
														+ ""
														+ dtb.getId();
												batch++;
												new File(batchDir).mkdir();
												numTestInDir = 0;
											}
											String shortZone = z.getId().split(
													":")[0];
											String callerName;
											if (theCaller.getName() == null) {
												callerName = theCaller
														.getMsisdn().toString();
											} else {
												callerName = theCaller
														.getName();
											}
											String TestName = bix
													.getDataBatchGroup()
													.getId()
													+ shortZone
													+ s[0].substring(2)
													+ callerName
													+ "to"
													+ dtb.getName();
											rte.generateTest(batchDir
													+ "/"
													+ GenericUtil
															.MakePrefix(testNumber)
													+ ":" + TestName
													+ +testNumber);
											testNumber++;
											GenericUtil
													.LOG(GenericUtil
															.MakePrefix(testNumber - 1)
															+ "]", 1,
															testNumber % 10);
											// rte.generateDocData(testNumber -
											// 1,
											// TestSuiteName, z.getId(),
											// dtb.getId(), s[1],
											// dtb.getBundle(), dataParm);
											rte.generateDocData(testNumber,
													TestSuiteName, TestName,
													shortZone, isNew);
											isNew = true;

										}
									}
									if (singleTest)
										break;

								}
							}
						}
					}
				}
				GenericUtil.LOG("", 1, 0);
			}

			if (bix.getVoiceBatchGroup() != null) {

				boolean isNew = false;

				// load called into banlabels maps
				// von:447570711205, 447570711206
				// offnBundle10:447570711204
				// offn:447570711204
				// vonBundle10:447570711205,447570711206

				HashMap<String, ArrayList<Called>> BANDS_B = new HashMap<String, ArrayList<Called>>();
				loadBANDS_B(bix.getVoiceBatchGroup().getCalled(), BANDS_B);

				for (org.sipsl.gisystems.rte.V2.TestsuiteV2.VoiceBatchGroup.Caller aparty : bix
						.getVoiceBatchGroup().getCaller()) {

					// transform the range into a list of numbers
					org.sipsl.gisystems.rte.V2.TestsuiteV2.VoiceBatchGroup.Caller theCaller = null;
					int elementNumber = 0;
					int capCaller = 0;
					ArrayList<org.sipsl.gisystems.rte.V2.TestsuiteV2.VoiceBatchGroup.Caller> CiArLi = null;
					if (aparty.getRange() != null) {
						CiArLi = getRangeVoice(aparty);
						capCaller = CiArLi.size();
					} else {
						theCaller = aparty;
					}


				// loop on zones
				for (org.sipsl.gisystems.rte.V2.TestsuiteV2.VoiceBatchGroup.Zone z : bix
						.getVoiceBatchGroup().getZone()) {

					int callingZone = 16;
					if (z.getId().startsWith("home")) {
						callingZone = 16;
					} else if (z.getId().startsWith("eu")
							|| z.getId().startsWith("row")) {
						callingZone = 17;
					}

					ArrayList<String> zoneNEV = new ArrayList<String>();
					ArrayList<String> zoneNES = new ArrayList<String>();

					loadNeZone(z.getId(), zoneNEV, "VOICEVLR");
					loadNeZone(z.getId(), zoneNES, "VOICESTP");

					// load voiceBatches from the current zone
					HashMap<String, VoiceBatch> NTW = new HashMap<String, VoiceBatch>();
					for (VoiceBatch vb : z.getVoiceBatch()) {
						// load from surepay.xml if referenced
						if (vb.getVoiceTariff().get(0).getReference() != null) {
							for (VoiceBatch vts : surepayReference
									.getVoiceBatchGroup().getZone().get(0)
									.getVoiceBatch()) {
								if (vts.getVoiceTariff() == null) {
									continue;
								}
								// TODO bug if tariff is after a bundle in
								// surepay.xml
								GenericUtil.LOG(vts.getVoiceTariff().get(0)
										.getReference(), ShowMe, 0);
								GenericUtil.LOG(vb.getVoiceTariff().get(0)
										.getReference(), ShowMe, 0);
								if (vts.getVoiceTariff() != null
										&& vts.getVoiceTariff()
												.get(0)
												.getReference()
												.compareTo(
														vb.getVoiceTariff()
																.get(0)
																.getReference()) == 0) {
									// remove first tariff line with the
									// reference tag
									VoiceTariff tts = cloner.deepClone(vts
											.getVoiceTariff().get(1));
									GenericUtil.LOG(vb.getId(), ShowMe, 0);
									vb.getVoiceTariff().remove(0);
									vb.getVoiceTariff().add(tts);

									NTW.put(vb.getId(), vb);
									break;
								}
							}
						} else {
							NTW.put(vb.getId(), vb);
						}
					}

						for (VoiceBatch vb : z.getVoiceBatch()) {
							subBatch = 0;
							numTestInDir = 0;
							batchDir = GenericUtil.MakePrefix(batch) + ":"
									+ subBatch + "" + bix.getId() + ""
									+ vb.getId();
							new File(batchDir).mkdir();

							// get the called number from BANDS_B(batch)
							for (Called bparty : BANDS_B.get(vb.getId())) {
								int i = 0;
								for (String netEl : zoneNEV) {
									String[] v = netEl.split("#");
									String[] s = zoneNES.get(i++).split("#");

									// euEIT1 in the test name
									String zoneName = z.getId().split(":")[0]
											+ v[0].substring(2);
									GenericUtil.LOG("zoneName " + zoneName,
											ShowZone, 0);

									GenericUtil.LOG("v[0] v[1] [" + v[0] + "]["
											+ v[1] + "]", ShowZone, 0);
									GenericUtil.LOG("s[0] s[1] [" + s[0] + "]["
											+ s[1] + "]", ShowZone, 0);

									// load parms
									params.put("VLR", v[1]);
									params.put("STP", s[1]);
									VoiceMoParams vp = new VoiceMoParams(params);

									// get duration list, pass it to test
									// creator along with index (position of
									// duration to consider)
									int Testindex = bix.getVoiceBatchGroup()
											.getDuration().size() + 3; // old
																		// stuff
																		// used
																		// when
																		// duration
																		// is
																		// calculated
																		// automatically
									for (int index = 4; index <= Testindex; index++) {

										if (CiArLi != null) {
											theCaller = CiArLi
													.get(elementNumber);
											elementNumber++;
											elementNumber = elementNumber
													% capCaller;

											vp.setMSISDN(theCaller.getMsisdnA()
													.toString());
											vp.setIMSI(theCaller.getImsiA() == null ? ""
													: theCaller.getImsiA()
															.toString());
											vp.setNOA(theCaller.getNoa() == null ? ""
													: theCaller.getNoa()
															.toString());
										} else {

											vp.setMSISDN(theCaller.getMsisdnA()
													.toString());
											vp.setIMSI(theCaller.getImsiA() == null ? ""
													: theCaller.getImsiA()
															.toString());
											vp.setNOA(theCaller.getNoa() == null ? ""
													: theCaller.getNoa()
															.toString());

										}

										vp.setBMSISDN(bparty.getMsisdnB()
												.toString());
										vp.setBIMSI(bparty.getImsiB() == null ? ""
												: bparty.getImsiB().toString());
										vp.setBNOA(bparty.getNoa() == null ? ""
												: bparty.getNoa().toString());

										GenericUtil.LOG("[", 1, 1);

										RteVoiceMoTestUnit rte = new RteVoiceMoTestUnit(
												bix,
												surepayReference,
												vp,
												aparty,
												bparty,
												NTW.get(vb.getId()),// batch
																	// need
																	// surepay.xml
												// NTW.get(vb.getId()).getVoiceBundle(),
												index, bix.getVoiceBatchGroup()
														.getDuration(),
												bix.getSubscriberType(),
												callingZone, cloner);
										numTestInDir++;
										if (numTestInDir > 20) {
											subBatch++;
											batch++;
											batchDir = GenericUtil
													.MakePrefix(batch)
													+ ":"
													+ subBatch
													+ ""
													+ bix.getId()
													+ ""
													+ vb.getId();
											new File(batchDir).mkdir();
											numTestInDir = 0;
										}
										String TestName = bix
												.getVoiceBatchGroup().getId()
												.replace("-", "")
												+ zoneName
												+ ""
												+ aparty.getId()
												+ "to"
												+ bparty.getId();
										rte.generateTest(batchDir
												+ "/"
												+ GenericUtil
														.MakePrefix(testNumber)
												+ ":" + TestName);
										GenericUtil.LOG(
												GenericUtil
														.MakePrefix(testNumber)
														+ "]", 1,
												(testNumber + 1) % 10);

										// BRS ID : test number
										// Subject : Topic of test :
										// <testsuiteV2 id="TestVoiceV3">
										// Test Name : Name of test : testName
										// Test Objective :Verify that a call of
										// 60 secs from a to bl costs ...
										// Step Name (Design Steps) : subscribe
										// to, make a call from a to b of tot
										// secs
										// Description (Design Steps) things to
										// verify (ama etc...)
										rte.generateDocVoice(testNumber,
												TestSuiteName, TestName,
												zoneName, isNew);
										isNew = true;
										testNumber++;
									}
								}
							}
							batch++;
						}
					}
				}
			}
			GenericUtil.LOG("", 1, 0);

		} catch (Exception eee) {
			eee.printStackTrace();
		}

	}

	private static ArrayList<Caller> getRange(Caller callerRange) {
		// TODO Auto-generated method stub
		// range = 1000, 0->999
		int ShowMe = 0;
		Integer cap = new Integer(callerRange.getRange());
		ArrayList<Caller> t = new ArrayList<Caller>(cap);

		for (int i = 0; i < cap; i++) {
			Caller caller = new Caller();
			caller.setImsi(callerRange.getImsi());
			caller.setName(callerRange.getName());
			BigDecimal ii = new BigDecimal(i);
			BigDecimal bd = callerRange.getMsisdn().add(ii);
			caller.setMsisdn(bd);
			GenericUtil.LOG("Number " + caller.getMsisdn(), ShowMe, 0);
			t.add(caller);
		}

		return t;
	}

	private static ArrayList<org.sipsl.gisystems.rte.V2.TestsuiteV2.VoiceBatchGroup.Caller> getRangeVoice(
			org.sipsl.gisystems.rte.V2.TestsuiteV2.VoiceBatchGroup.Caller callerRange) {
		// TODO Auto-generated method stub
		// range = 1000, 0->999
		int ShowMe = 0;
		Integer cap = new Integer(callerRange.getRange());
		ArrayList<org.sipsl.gisystems.rte.V2.TestsuiteV2.VoiceBatchGroup.Caller> t = new ArrayList<org.sipsl.gisystems.rte.V2.TestsuiteV2.VoiceBatchGroup.Caller>(
				cap);

		for (int i = 0; i < cap; i++) {
			org.sipsl.gisystems.rte.V2.TestsuiteV2.VoiceBatchGroup.Caller caller = new org.sipsl.gisystems.rte.V2.TestsuiteV2.VoiceBatchGroup.Caller();
			caller.setImsiA(callerRange.getImsiA());
			caller.setId(callerRange.getId());
			BigDecimal ii = new BigDecimal(i);
			BigDecimal bd = new BigDecimal(callerRange.getMsisdnA()).add(ii);
			caller.setMsisdnA(bd.toString());
			GenericUtil.LOG("Number " + caller.getMsisdnA(), ShowMe, 0);
			t.add(caller);
		}

		return t;
	}

	private static void loadBANDS_B(List<Called> called,
			HashMap<String, ArrayList<Called>> band_b) {
		// band1,b1,b2,b3
		// band3,b4,
		// ..

		int seeMe = 0;
		for (Called cd : called) {
			String[] band = cd.getBandLabel().split(",");
			for (String b : band) {
				if (band_b.containsKey(b)) {
					// queue into array
					band_b.get(b).add(cd);
				} else {
					ArrayList<Called> t = new ArrayList<Called>();
					t.add(cd);
					band_b.put(b, t);
				}
			}
		}
		// dump
		if (seeMe != 0) {
			Iterator it = band_b.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry pairs = (Map.Entry) it.next();
				for (Called s : (ArrayList<Called>) pairs.getValue()) {
					GenericUtil.LOG(
							(String) pairs.getKey() + ":" + s.getMsisdnB(), 1,
							0);
				}
			}
		}
	}

	private static void loadNeZone(String z, ArrayList<String> zoneNE, String ne) {
		// length 3 is data
		// length 4 is voice
		if (z.startsWith("home")) {
			if (z.length() > 4) {
				fetchNE(zoneNE, "HOME" + ne, z.substring(4));
			} else {
				fetchNE(zoneNE, "HOME" + ne, "");
			}
		}
		if (z.startsWith("eu")) {
			if (z.length() > 2) {
				fetchNE(zoneNE, "EU" + ne, z.substring(3));
			} else {
				fetchNE(zoneNE, "EU" + ne, "");
			}
		} else if (z.startsWith("row")) {
			if (z.length() > 3) {
				fetchNE(zoneNE, "ROW" + ne, z.substring(4));
			} else {
				fetchNE(zoneNE, "ROW" + ne, "");

			}
		}

	}

	public static boolean enqRteOption(TestsuiteV2 bix, String option) {

		if (bix.getSubscriberType() != null) {
			for (Rteoptions op : bix.getSubscriberType().getRteoptions()) {
				if (op.getKey().compareTo(option) == 0) {
					return true;
				}
			}
		}
		return false;
	}

	private static void fetchDefaults(Map<String, String> params,
			String fileName) {
		try {
			JAXBContext cate2 = JAXBContext
					.newInstance("org.sipsl.gisystems.rte.def");
			Unmarshaller u2 = cate2.createUnmarshaller();
			Rtedefaults def = (Rtedefaults) u2.unmarshal(new FileInputStream(
					fileName));

			for (PropertyKey p : def.getProperties().getPropertyKey()) {
				for (PropertyValue pv : p.getPropertyValue()) {
					params.put(
							pv.getKey()
									+ ((pv.getName() != null) ? "_"
											+ pv.getName() : ""), pv.getValue());
				}
			}

			dump(params);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void fetchNE(ArrayList<String> eusgsn, String zone,
			String countries) {
		int seeMe = 0;
		try {

			GenericUtil.LOG("Zone " + zone + " " + countries + " ", seeMe, 0);
			JAXBContext cate2 = JAXBContext
					.newInstance("org.sipsl.gisystems.rte.def");
			Unmarshaller u2 = cate2.createUnmarshaller();
			Rtedefaults def = (Rtedefaults) u2.unmarshal(new FileInputStream(
					"default.xml"));

			// eu:EIT only all italy sgsn
			// eu:EIT1 only IT 1
			// eu:EIT,ETK only alli italy and all turkey
			// eu:EIT,ETK5 only all italy and Turkey 5
			// eu:-:EIT all except italy
			// eu:-:EIT1 all except italy 1
			// eu:-:EIT1,ETK all except italy one and not turkey
			if (countries.length() > 0 && !countries.startsWith("-")) {
				String[] toks = countries.split(",");
				for (String item : toks) {
					GenericUtil.LOG("item " + item + " - " + item.length()
							+ " - " + 4, seeMe, 0);
					if (item.length() > 4) {
						GenericUtil.PICO(582, seeMe);
						// EIT25 or
						// take one, the selected NODE
						for (PropertyKey p : def.getProperties()
								.getPropertyKey()) {
							if (p.getId().compareTo(zone) == 0) {
								for (PropertyValue pv : p.getPropertyValue()) {
									GenericUtil.LOG("pv.getName() "
											+ pv.getName().substring(2) + " - "
											+ item.substring(1), seeMe, 0);
									if (pv.getName().substring(2)
											.compareTo(item.substring(1)) == 0) {
										eusgsn.add(pv.getName() + "#"
												+ pv.getValue());
									}
								}
							}
						}
					} else {
						// IT,
						// take all
						for (PropertyKey p : def.getProperties()
								.getPropertyKey()) {

							if (p.getId().compareTo(zone) == 0) {
								GenericUtil.PICO(600, seeMe);
								for (PropertyValue pv : p.getPropertyValue()) {
									GenericUtil.PICO(602, seeMe);
									GenericUtil.LOG(
											"pv.getName() " + pv.getName()
													+ " - " + item, seeMe, 0);
									GenericUtil.LOG("pv.getName() "
											+ pv.getName().substring(2) + " - "
											+ item.substring(1), seeMe, 0);
									if (pv.getName().substring(2)
											.startsWith(item.substring(1))) {
										GenericUtil.PICO(598, seeMe);
										eusgsn.add(pv.getName() + "#"
												+ pv.getValue());
									}
								}
							}
						}
					}
				}
			} else if (countries.length() > 0 && countries.startsWith("-")) {
				GenericUtil.PICO(615, seeMe);
				String noMinus = countries.substring(2);
				// Load all and remove
				for (PropertyKey p : def.getProperties().getPropertyKey()) {
					if (p.getId().compareTo(zone) == 0) {
						for (PropertyValue pv : p.getPropertyValue()) {
							GenericUtil
									.LOG("map " + pv.getName() + "#"
											+ pv.getValue(), seeMe, 0);
							eusgsn.add(pv.getName() + "#" + pv.getValue());
						}
					}
				}
				GenericUtil.LOG("minus " + noMinus, seeMe, 0);
				String[] toks = noMinus.split(",");
				for (String item : toks) {
					GenericUtil.PICO(629, seeMe);
					if (item.length() > 4) {
						// EIT25
						// take one, the selected SGSN
						GenericUtil.PICO(633, seeMe);
						for (PropertyKey p : def.getProperties()
								.getPropertyKey()) {
							if (p.getId().compareTo(zone) == 0) {
								for (PropertyValue pv : p.getPropertyValue()) {
									GenericUtil.LOG("637 "
											+ pv.getName().substring(2) + " "
											+ item.substring(1), seeMe, 0);
									if (pv.getName().substring(2)
											.startsWith(item.substring(1))) {
										eusgsn.remove(pv.getName() + "#"
												+ pv.getValue());
									}
								}
							}
						}
					} else {
						GenericUtil.PICO(644, seeMe);
						// IT,
						// take all
						for (PropertyKey p : def.getProperties()
								.getPropertyKey()) {
							GenericUtil.LOG("645 " + p.getId() + " " + zone,
									seeMe, 0);
							if (p.getId().compareTo(zone) == 0) {
								GenericUtil.PICO(646, seeMe);
								for (PropertyValue pv : p.getPropertyValue()) {
									GenericUtil.LOG("652 "
											+ pv.getName().substring(2) + " "
											+ item.substring(1), seeMe, 0);
									if (pv.getName().substring(2)
											.startsWith(item.substring(1))) {
										eusgsn.remove(pv.getName() + "#"
												+ pv.getValue());
									}
								}
							}
						}
					}
				}
			} else {
				for (PropertyKey p : def.getProperties().getPropertyKey()) {
					if (p.getId().compareTo(zone) == 0) {
						for (PropertyValue pv : p.getPropertyValue()) {
							// not nice...
							eusgsn.add(pv.getName() + "#" + pv.getValue());
						}
					}

				}
			}
			dump2(eusgsn);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// private static void fetchSTPVLR(ArrayList<String> eusgsn, String zone) {
	// try {
	// JAXBContext cate2 = JAXBContext
	// .newInstance("org.sipsl.gisystems.rte.def");
	// Unmarshaller u2 = cate2.createUnmarshaller();
	// Rtedefaults def = (Rtedefaults) u2.unmarshal(new FileInputStream(
	// "default.xml"));
	//
	// for (PropertyKey p : def.getProperties().getPropertyKey()) {
	// if (p.getId().compareTo(zone) == 0) {
	// for (PropertyValue pv : p.getPropertyValue()) {
	// // not nice...
	// eusgsn.add(pv.getName() + "#" + pv.getValue());
	// }
	// }
	//
	// }
	// dump2(eusgsn);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }

	private static void dump(Map<String, String> params) {
		for (Map.Entry<String, String> s : params.entrySet()) {
			GenericUtil.LOG("Map [" + s.getKey() + "] :: [" + s.getValue()
					+ "]", 0, 0);
		}
	}

	private static void dump2(ArrayList<String> params) {
		int seeMe = 0;
		for (String s : params) {
			GenericUtil.LOG("List [" + s + "]", seeMe, 0);
		}
	}

}
